package com.gu.ExcelUtils;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.gu.entity.VehicleRefuelingInfo;
import com.gu.service.VehicleRefuelingInfoService;
import com.gu.service.impl.VehicleRefuelingInfoServiceImpl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * @Author: gusongliao
 * @Description: 车辆加油读取excel监听类
 * @createDate 2023/01/05 22:20
 */
public class VehicleRefuelingInfoReadListener implements ReadListener {
    private final static Logger LOGGER = Logger.getLogger(VehicleRefuelingInfoServiceImpl.class);

    @Autowired
    private VehicleRefuelingInfoService vehicleRefuelingInfoService;

    @Override
    public void invoke(Object o, AnalysisContext analysisContext) {
        // 读取到的数据就放在Object o的o里，里面就是User类型的数据，可以强制转换成User类型
        try {
            VehicleRefuelingInfo vehicleRefuelingInfo = (VehicleRefuelingInfo) o;
            vehicleRefuelingInfo.setCreatetimestamp(new Date());
            vehicleRefuelingInfo.setUpdatetimestamp(new Date());
            vehicleRefuelingInfoService.insertSelective(vehicleRefuelingInfo);
            System.out.println(vehicleRefuelingInfo);
        } catch (Exception e) {
            LOGGER.error(e);
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("已经将文件处理完毕");
    }
}
